import { NextRequest, NextResponse } from "next/server";
import { SqliteDatabase } from "@/lib/sqlite-database";

export async function GET(request: NextRequest) {
  try {
    const database = new SqliteDatabase();
    
    // 使用getAllDevices方法获取设备数量
    const allDevices = database.getAllDevices();
    const deviceCount = allDevices.length;
    
    // 直接使用简单的查询方法
    const stats = database.getDashboardStats();
    
    // 获取所有设备ID
    const allDeviceIds = allDevices.map(d => d.id!);
    
    // 手动检查每个参数表的覆盖情况
    const namingDeviceIds: number[] = [];
    const positionDeviceIds: number[] = [];
    const baseDeviceIds: number[] = [];
    
    let namingCount = 0;
    let positionCount = 0;
    let baseCount = 0;
    
    // 遍历所有设备，检查参数覆盖情况
    for (const device of allDevices) {
      const deviceId = device.id!;
      
      // 检查命名参数
      const namingParams = database.getNamingParametersByDeviceId(deviceId);
      if (namingParams.length > 0) {
        namingDeviceIds.push(deviceId);
        namingCount += namingParams.length;
      }
      
      // 检查位置参数
      const positionParams = database.getPositionParametersByDeviceId(deviceId);
      if (positionParams.length > 0) {
        positionDeviceIds.push(deviceId);
        positionCount += positionParams.length;
      }
      
      // 检查基础参数
      const baseParams = database.getBaseParametersByDeviceId(deviceId);
      if (baseParams.length > 0) {
        baseDeviceIds.push(deviceId);
        baseCount += baseParams.length;
      }
    }
    
    // 检查缺失的设备ID
    const missingNamingDevices = allDeviceIds.filter(id => !namingDeviceIds.includes(id));
    const missingPositionDevices = allDeviceIds.filter(id => !positionDeviceIds.includes(id));
    const missingBaseDevices = allDeviceIds.filter(id => !baseDeviceIds.includes(id));
    
    // 计算范围
    const namingRange = namingDeviceIds.length > 0 ? {
      min_id: Math.min(...namingDeviceIds),
      max_id: Math.max(...namingDeviceIds)
    } : { min_id: null, max_id: null };
    
    const positionRange = positionDeviceIds.length > 0 ? {
      min_id: Math.min(...positionDeviceIds),
      max_id: Math.max(...positionDeviceIds)
    } : { min_id: null, max_id: null };
    
    const baseRange = baseDeviceIds.length > 0 ? {
      min_id: Math.min(...baseDeviceIds),
      max_id: Math.max(...baseDeviceIds)
         } : { min_id: null, max_id: null };
    
    return NextResponse.json({
      success: true,
      data: {
        totalStats: {
          devices: deviceCount,
          namingParameters: namingCount,
          positionParameters: positionCount,
          baseParameters: baseCount
        },
        ranges: {
          naming: namingRange,
          position: positionRange,
          base: baseRange
        },
        deviceCoverage: {
          naming: {
            count: namingDeviceIds.length,
            devices: namingDeviceIds,
            missing: missingNamingDevices
          },
          position: {
            count: positionDeviceIds.length,
            devices: positionDeviceIds,
            missing: missingPositionDevices
          },
          base: {
            count: baseDeviceIds.length,
            devices: baseDeviceIds,
            missing: missingBaseDevices
          }
        },
        issues: {
          incompleteCoverage: {
            naming: missingNamingDevices.length > 0,
            position: missingPositionDevices.length > 0,
            base: missingBaseDevices.length > 0
          },
          missingDeviceCounts: {
            naming: missingNamingDevices.length,
            position: missingPositionDevices.length,
            base: missingBaseDevices.length
          }
        }
      }
    });
    
  } catch (error) {
    console.error("检查参数数据时出错:", error);
    return NextResponse.json(
      { 
        success: false, 
        message: "检查参数数据过程中发生错误",
        error: error instanceof Error ? error.message : "未知错误"
      },
      { status: 500 }
    );
  }
} 